package com.example.shop.cate

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.CategoryEntity
import com.example.base.entity.GoodsEntity
import com.example.base.mvvm.BaseFragment
import com.example.shop.R
import com.example.shop.adapter.CateChildAdapter
import com.example.shop.adapter.CateParentAdapter
import com.example.shop.adapter.GoodsAdapter
import com.example.shop.databinding.FragmentCateBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CateFragment : BaseFragment<FragmentCateBinding,CateViewModel>() {
    val onelist = mutableListOf<CategoryEntity>()
    lateinit var oneAdapter:CateParentAdapter

    val twolist = mutableListOf<CategoryEntity>()
    lateinit var twoAdapter:CateChildAdapter

    val goodslist = mutableListOf<GoodsEntity>()
    lateinit var goodsAdapter: GoodsAdapter

    var cateId = 0
    var page = 1
    override fun initView() {
        oneAdapter = CateParentAdapter(onelist)
        binding.cateParentRv.adapter = oneAdapter
        binding.cateParentRv.layoutManager = LinearLayoutManager(context)
        viewModel.cate()
        //点击一级分类item
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.setSelectedItem(onelist[position].id)
            twolist.clear()
            twoAdapter.notifyDataSetChanged()
            //重新加载二级分类
            val data = onelist[position].data
            if(data.isNotEmpty()){

                twolist.clear()
                twolist.addAll(data)
                twoAdapter.notifyDataSetChanged()
                twoAdapter.setSelectedItem(twolist[0].id)
                if(twolist.isNotEmpty()){
                    cateId = twolist[0].id
                    page = 1
                    viewModel.goods(cateId,page,10)
                }
            }

        }

        twoAdapter = CateChildAdapter(twolist)
        binding.cateChildRv.adapter = twoAdapter
        binding.cateChildRv.layoutManager = LinearLayoutManager(context)
        //二级分类点击事件
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            twoAdapter.setSelectedItem(twolist[position].id)
            if(twolist.isNotEmpty()){
                cateId = twolist[position].id
                page=1
                viewModel.goods(cateId,page,10)
            }
        }

        goodsAdapter = GoodsAdapter(goodslist)
        binding.goodsRv.adapter = goodsAdapter
        binding.goodsRv.layoutManager = LinearLayoutManager(context)

        binding.smart.setOnRefreshLoadMoreListener(object:OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                viewModel.goods(cateId,page,10)
                refreshLayout.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.goods(cateId,page,10)
                refreshLayout.finishLoadMore()
            }
        })
        //三级分类点击跳转到详情界面
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodslist[position].id).navigation()
        }

    }

    override fun initData() {
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                //加载一级分类
                onelist.clear()
                onelist.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //加载二级分类
                val data = it.data[0].data
                if(data.isNotEmpty()){
                    //清空之前的数据
                    twolist.clear()
                    twolist.addAll(data)
                    twoAdapter.notifyDataSetChanged()
                    //请求三级分类----加载商品列表
                    cateId = data[0].id
                    viewModel.goods(cateId,page,10)

                }
            }
        }

        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodslist.clear()
                }
                goodslist.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }

    }

    override fun getViewModel(): Class<CateViewModel> = CateViewModel::class.java

    override fun getLayout(): Int = R.layout.fragment_cate

}